package de.unibremen.mum.app.whatscool;

import java.io.File;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener, HomeFragment.OnHeadlineSelectedListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the four primary sections of the app. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     * <p/>
     * the following is referring to: http://developer.android.com/shareables/training/EffectiveNavigation.zip
     */
    private AppSectionsPagerAdapter mAppSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will display the four primary sections of the
     * app, one at a time.
     */
    private ViewPager mViewPager;
    
    public static final String INTENT_EXTRA_URL = "photoUrl";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);       

        // Create the adapter that will return a fragment for each of the four
        // primary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();

        // Specify that the Home/Up button should not be enabled, since there is no hierarchical
        // parent.
        actionBar.setHomeButtonEnabled(false);

        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager, attaching the adapter and setting up a listener
        // for when the
        // user swipes between sections.
        //mViewPager.setOffscreenPageLimit(8);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select
                // the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if
                // we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter.
            // Also specify this Activity object, which implements the
            // TabListener interface, as the
            // listener for when this tab is selected.
            ActionBar.Tab tab = actionBar.newTab();

            switch (i) {
                case 0:
                    tab.setText("Home");
                    break;
                case 1:
                    tab.setText("Gallerie");
                    break;
            }
            // tab.setIcon(R.drawable.ic_tabshome)
            tab.setTabListener(this);
            actionBar.addTab(tab);
        }
        // custom layout:
        actionBar.setDisplayShowTitleEnabled(false); // if you dont want title
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                ActionBar.DISPLAY_SHOW_CUSTOM);
        // actionBar.setCustomView(R.layout.action_bar_custom_view);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.about) {
            Intent intent = new Intent(this, InfoActivity.class);
            startActivity(intent);
            return true;
        } else
            return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
                              FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) {
    }

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        // The FragmentManager is the interface for working with fragments in
        // Java code you use it to find, add, remove, and replace fragments.
        // Fragments can be referenced by their ID or by a tag string.
        // When the FragmentPagerAdapter adds a fragment to the FragmentManager,
        // it uses a special tag based on the particular position that the
        // fragment will be placed.
        FragmentManager fragmentManager;

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
            fragmentManager = fm;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new Fragment();

            switch (position) {
                case 0:
                    return fragment = new HomeFragment();
                case 1:
                    return fragment = new GalleryFragment();
                default:
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "Section " + (position + 1);
        }
    }

    /**
     * Wird von dem HomeFragment aufgerufen, nachdem ein Foto geschossen wurde
     * @param url Die URL des geschossenen Fotos
     */
    @Override
    public void onPhotoTaken(String url) {
        //Öffne Bewertung und übergebe url
    	if(new File(url).exists()){
        	Intent intent = new Intent(MainActivity.this, RatingActivity.class);
        	intent.putExtra(INTENT_EXTRA_URL, url);
        	startActivity(intent);
    	}
    }
}
